#   库的引用
import sys
import sqlite3
import os.path

import PyQt6.QtWidgets as QtWidgets
import PyQt6.QtGui as QtGui
import PyQt6.QtCore as QtCore

import today
import add
import task
import calendar1
import setting

#   定义主界面类
class MainWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    #   创建窗口
    def initUI(self):
        #   窗口属性
        self.resize(800, 600)
        self.setWindowTitle("计划清单 v0.1")
        
        self.main_Layout = QtWidgets.QHBoxLayout()
        self.main_Layout.setContentsMargins(0, 0, 0, 0)
        self.main_Layout.setSpacing(0)
        self.setLayout(self.main_Layout)

        #   左侧导航栏
        self.left_Widget = QtWidgets.QWidget()
        self.Left_Layout = QtWidgets.QVBoxLayout()
        self.Left_Layout.setContentsMargins(6, 6, 3, 6)
        self.Left_Layout.setSpacing(6)
        self.left_Widget.setLayout(self.Left_Layout)
        self.main_Layout.addWidget(self.left_Widget)

        self.btnToday = QtWidgets.QPushButton(" 今日事件")
        self.btnToday.setStyleSheet("font-size: 16px")
        self.btnToday.setIcon(QtGui.QIcon("Icon/today.png"))
        self.btnToday.setCheckable(True)
        self.btnToday.setChecked(True)
        self.btnToday.clicked.connect(self.display)
        self.Left_Layout.addWidget(self.btnToday)

        self.btnNew = QtWidgets.QPushButton(" 添加任务")
        self.btnNew.setStyleSheet("font-size: 16px")
        self.btnNew.setIcon(QtGui.QIcon("Icon/add.png"))
        self.btnNew.setCheckable(True)
        self.btnNew.clicked.connect(self.display)
        self.Left_Layout.addWidget(self.btnNew)

        self.btnTask = QtWidgets.QPushButton(" 任务管理")
        self.btnTask.setStyleSheet("font-size: 16px")
        self.btnTask.setIcon(QtGui.QIcon("Icon/task.png"))
        self.btnTask.setCheckable(True)
        self.btnTask.clicked.connect(self.display)
        self.Left_Layout.addWidget(self.btnTask)

        self.btnCalendar = QtWidgets.QPushButton(" 日历视图")
        self.btnCalendar.setStyleSheet("font-size: 16px")
        self.btnCalendar.setIcon(QtGui.QIcon("Icon/calendar.png"))
        self.btnCalendar.setCheckable(True)
        self.btnCalendar.clicked.connect(self.display)
        self.Left_Layout.addWidget(self.btnCalendar)
        self.Left_Layout.addStretch(1)

        self.btnSetting = QtWidgets.QPushButton(" 软件设置")
        self.btnSetting.setStyleSheet("font-size: 16px")
        self.btnSetting.setIcon(QtGui.QIcon("Icon/setting.png"))
        self.btnSetting.setCheckable(True)
        self.btnSetting.clicked.connect(self.display)
        self.Left_Layout.addWidget(self.btnSetting)

        #   右侧主窗口
        self.right_Widget = QtWidgets.QStackedWidget()
        self.main_Layout.addWidget(self.right_Widget)

        #   右侧主窗口_今日页面
        self.window_Today = today.Today()
        self.right_Widget.addWidget(self.window_Today)

        #   右侧主窗口_添加任务
        self.window_Add = add.AddTask()
        self.window_Add.btnOK.clicked.connect(self.newTask)
        self.right_Widget.addWidget(self.window_Add)
        
        #   右侧主窗口_任务管理
        self.window_Task = task.TaskManager()
        self.window_Task.btnDel.clicked.connect(self.delTask)
        self.right_Widget.addWidget(self.window_Task)

        self.updateTask()

        #   右侧主窗口_日历视图
        self.window_Calendar = calendar1.Calendar()
        self.right_Widget.addWidget(self.window_Calendar)

        #   右侧主窗口_软件设置
        self.window_Setting = setting.Setting()
        self.right_Widget.addWidget(self.window_Setting)
        
        #   显示窗口
        self.show()

    #   切换界面
    def display(self):
        sender = self.sender()
        if sender.text() == " 今日事件":
            self.right_Widget.setCurrentIndex(0)
            self.btnToday.setChecked(True)
            self.btnNew.setChecked(False)
            self.btnTask.setChecked(False)
            self.btnCalendar.setChecked(False)
            self.btnSetting.setChecked(False)
        if sender.text() == " 添加任务":
            self.right_Widget.setCurrentIndex(1)
            self.btnToday.setChecked(False)
            self.btnNew.setChecked(True)
            self.btnTask.setChecked(False)
            self.btnCalendar.setChecked(False)
            self.btnSetting.setChecked(False)
        if sender.text() == " 任务管理":
            self.right_Widget.setCurrentIndex(2)
            self.btnToday.setChecked(False)
            self.btnNew.setChecked(False)
            self.btnTask.setChecked(True)
            self.btnCalendar.setChecked(False)
            self.btnSetting.setChecked(False)

            self.updateTask()
        if sender.text() == " 日历视图":
            self.right_Widget.setCurrentIndex(3)
            self.btnToday.setChecked(False)
            self.btnNew.setChecked(False)
            self.btnTask.setChecked(False)
            self.btnCalendar.setChecked(True)
            self.btnSetting.setChecked(False)
        if sender.text() == " 软件设置":
            self.right_Widget.setCurrentIndex(4)
            self.btnToday.setChecked(False)
            self.btnNew.setChecked(False)
            self.btnTask.setChecked(False)
            self.btnCalendar.setChecked(False)
            self.btnSetting.setChecked(True)

    #   刷新任务
    def updateTask(self):
        BASE_DIR = os.path.dirname(os.path.abspath(__file__))
        db_path = os.path.join(BASE_DIR, "Data/data.db")
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        cursor.execute("create table if not exists tasks (id nvarchar(10) primary key, title nchar(10), describe nchar(10), date nchar(10))")
        cursor.execute("select * from tasks where id>0")
        values = cursor.fetchall()
        self.window_Task.task_List.clear()
        for i in values:
            self.window_Task.addItem(i[0], i[1], i[2])
        cursor.close()
        conn.close()

    
    #   添加任务
    def newTask(self):
        if self.window_Add.lineTitle.text() == "":
            self.window_Add.labStatus.setStyleSheet("font-size: 16px; color: red")
            self.window_Add.labStatus.setText("————内务内容为空————")
            QtCore.QTimer.singleShot(1000, self.window_Add.initialize)
            return 0
        
        info = self.window_Add.report()
        file = open("Data/count.txt", "r")
        count = file.readline()
        id = int(count)+1
        file.close()
        file = open("Data/count.txt", "w")
        file.write(str(id))
        file.close()

        BASE_DIR = os.path.dirname(os.path.abspath(__file__))
        db_path = os.path.join(BASE_DIR, "Data/data.db")
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        cursor.execute("create table if not exists tasks (id nvarchar(10) primary key, title nchar(10), describe nchar(10), date nchar(10))")
        cursor.execute("insert into tasks(id, title, describe) values(\'{0}\', \'{1}\', \'{2}\')".format(id, info["title"], info["describe"]))
        conn.commit()
        cursor.close()
        conn.close()

        #   提示与初始化
        self.window_Add.labStatus.setStyleSheet("font-size: 16px; color: green")
        self.window_Add.labStatus.setText("————任务创建成功————")
        QtCore.QTimer.singleShot(1000, self.window_Add.initialize)

    #   删除任务
    def delTask(self):     
        if self.window_Task.task_List.currentItem() == None:
            return 0
        else:
            selected_item = self.window_Task.task_List.currentItem()
            title = selected_item.text()
            id = self.window_Task.detail_Dict[title][0]
            self.window_Task.detail_Dict.pop(title)
            row = self.window_Task.task_List.currentRow()
            self.window_Task.task_List.takeItem(row)

            BASE_DIR = os.path.dirname(os.path.abspath(__file__))
            db_path = os.path.join(BASE_DIR, "Data/data.db")
            conn = sqlite3.connect(db_path)
            cursor = conn.cursor()
            cursor.execute("delete from tasks where id=?", (id))
            conn.commit()
            cursor.close()
            conn.close()

#   运行程序
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec())